﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;
using WomanMotion.WebApi.Api.Utility;
using WomanMotion.WebApi.Infrastructure.DataBaseCommon;
using WomanMotion.WebApi.Infrastructure.UnifyResult;
using WomanMotion.WebApi.Model.Dtos;
using WomanMotion.WebApi.Model.Dtos.Classify;
using WomanMotion.WebApi.Model.Dtos.SysConfig;
using WomanMotion.WebApi.Model.Entitys;
using WomanMotion.WebApi.Service;
using WomanMotion.WebApi.Service.IService;

namespace WomanMotion.WebApi.Api.Controllers
{
    /// <summary>
    /// 系统参数管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [XSSFilter]
    [ApiController]
    [DescriptionName("系统参数管理", true, 10)]
    public class SysConfigController : BaseController
    {
        private readonly ISysConfigService _config;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="config"></param>
        public SysConfigController(ILogger<SysConfigController> logger, ISysConfigService config) : base(logger)
        {
            _config = config;
        }

        /// <summary>
        /// 创建系统参数
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<SysConfigVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("创建系统参数", true, "系统参数管理", "List", "SysConfigList")]
        public async Task<IResultOutput> Created(CreatedSysConfigDTO dto)
        {
            var res = await _config.CreatedAsync(dto);
            if (res != null)
            {
                return ResultOutput.Ok(res, "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 启用系统参数
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("启用系统参数")]
        public async Task<IResultOutput> Enable(PrimaryKeyDTO ids)
        {
            var res = await _config.UpdateDisableAsync(DisableEnum.Enable, ids.Ids);
            if (res > 0)
            {
                return ResultOutput.Ok(new object(), "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 禁用系统参数
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("禁用系统参数")]
        public async Task<IResultOutput> Disable(PrimaryKeyDTO ids)
        {
            var res = await _config.UpdateDisableAsync(DisableEnum.Disable, ids.Ids);
            if (res > 0)
            {
                return ResultOutput.Ok(new object(), "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 更新系统参数
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<SysConfigVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("更新系统参数")]
        public async Task<IResultOutput> Update(UpdateSysConfigDTO dto)
        {
            var res = await _config.UpdateAsync(dto);
            if (res == null)
            {
                return ResultOutput.NotOk("操作失败");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据ID获取系统参数
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<SysConfigVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据ID获取系统参数")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetById(long id)
        {
            var res = await _config.GetByIdAsync(id);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据ID获取多表连接系统参数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<SysConfigVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据ID获取多表连接系统参数")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetCommonById(long id)
        {
            var res = await _config.GetByIDNavAsync(id);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据name获取系统参数
        /// </summary>
        /// <param name="name">参数名</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<SysConfigVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据name获取系统参数")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetCommonByNmae(string name)
        {
            var res = await _config.GetAsync(m => m.Name == name);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 分页获取系统参数
        /// </summary>
        /// <param name="pars">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<PageViewBase<List<SysConfigVM>>>), (int)HttpStatusCode.OK)]
        [DescriptionName("分页获取系统参数")]
        public async Task<IResultOutput> GetPageList(QueryParameterBase pars)
        {
            var res = await _config.GetPageNavListAsync(pars);
            if (res.Data == null || res.Data.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }
    }
}